package tmproject.hlhj.fhp.tmvote.network

import android.content.Context
import android.support.v7.app.AlertDialog
import android.widget.Toast
import com.lzy.okgo.model.Response
import tmproject.hlhj.fhp.tmvote.BaseAty
import tmproject.hlhj.fhp.tmvote.beans.BaseBean
import tmproject.hlhj.fhp.tmvote.log
import tmproject.hlhj.fhp.tmvote.toast
import tmproject.hlhj.fhp.tmvote.utils.ToastUtil

/**
 * Created by Never Fear   on 2018\8\6 0006.
Never More....
 */
abstract class BaseObersever<T>(private val context: Context) :BaseAction<T> {
    override fun onHttpFailed() {
        //网络情请求失败时的处理
        AlertDialog.Builder(context)
                .setTitle("网络请求失败")
                .setMessage("请检查您的网络")
                .setPositiveButton("确定"){
                    dialog,bt->
                    dialog.dismiss()
                }
                .show()
    }

    override fun onRespSuccess(t: T) {

    }

    override fun onError(e: Throwable?) {
        super.onError(e)
        if (context is BaseAty<*,*>){
            context.loadingDialog.dismiss()
        }
        if (e?.toString()?.contains("JsonSyntaxException")!!){
            ToastUtil.toast("Gson解析数据时发生错误")
        }else{
            //网络情请求失败时的处理
            AlertDialog.Builder(context)
                    .setTitle("网络请求失败")
                    .setMessage("请检查您的网络")
                    .setPositiveButton("确定"){
                        dialog,bt->
                        dialog.dismiss()
                    }
                    .show()
        }

    }
    override fun onacheSuccess(t: T) {
    }
    override fun onRespErro(t: T) {
        if (context is BaseAty<*,*>){
            context.loadingDialog.dismiss()
        }
        val response = t as Response<*>
        val baseBean = t.body() as BaseBean
        ToastUtil.toast(baseBean.message)
        log("okgo","返回的Code不等于200了")
    }
}